<?php
/** 检票报表后端处理 本页面只是处理参数 然后发送到s.12301.cc的pft/order_report/product.php进行查询
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-8-13
 * Time: 上午9:34
 */
if($_SESSION['sid']!=1) exit('');
require("/var/www/html/new/conf/le.je");
$le=new go_sql();
$le->connect();
/*
$chenping_7 = array(
    //一组
    190=>1,
    220=>1,
    280=>3,//四川 陕西
    320=>3,//荣卫
    130=>4,//山西,安徽
    180=>4,//张彬
    310=>7,//云南,河南
    230=>7,//李凤
    370=>10,//蒙古 海南南
    270=>10,//王佳鑫
    2900=>'11|8',//上海 江苏
    160=>11,//陈增祥
    //二组
    380=>5,//广西,湖南
    250=>5,//陈平
    140=>6,//辽宁,广东
    260=>6,//叶长壮
    6530=>9,//重庆,湖北
    240=>9,//张全
    120=>2,//河北,浙江
    170=>2,//林静静
    1100=>8,//天津,宁夏
    390=>8,//邓玉萍
);
*/
$chenping = file_get_contents('chenping.json');
$group  = array(
    '1'=>'1,3,4,7,10,12,20,21,23,25',
    '2'=>'2,5,6,8,9,11,13,22,24'
);
/* 接受到js发来的参数
 * @param btime 开始时间
 * @param etime 结束时间
 * @param qiandan_post 搜索指定签单人
 * @param group 签单人分组
 * @param city 市
 * @param province 省
 * @param select_type 1 景区名称 2 企业名称
 * @param select_text 输入的文本
 * @param select_id   如果是点击下拉框 那么会有id 则不用上面传过来的文本
 * @param com_type  array 企业类型
 * */


/*发出去的参数 全都是$post开头的
 * btime    date
 * etime    date
 * qiandan  int   签单人id
 * aid      str   签单人签的供应商id 逗号隔开
 * tid      str   票类id
 * */

//echo '<pre>';
//print_r($_POST);
//echo '</pre>';exit;

//处理日期
$post_btime = chkDate($_POST['btime']);
$post_etime = chkDate($_POST['etime']);
$post_qiandan = $post_aid = $post_tid = $post_group = null;

function chkDate($date){
    $unixTime = strtotime($date);
    if (!$unixTime) {
        return date('Y-m-d',strtotime('yesterday'));
    }
    if (date('Y-m-d', $unixTime) ==  date('Y-m-d', strtotime($date))) {
        return  date('Y-m-d', strtotime($date));
    }
    return date('Y-m-d',strtotime('yesterday'));
}

$where_aid = 'where 1=1 ';
$where_lid = 'where 1=1 ';
//处理签单人
if($_POST['qiandan']+0>0){
    $post_qiandan = $_POST['qiandan'];
}elseif($_POST['group']+0>0){
    $post_group = $group[$_POST['group']];
}


//echo $where_aid;exit;
//传入景区名称或者企业名称.有ID查ID 没ID查文本
if($_POST['select_id']!=''){
    if($_POST['select_type']==1)
        $where_lid .=" and id={$_POST['select_id']} ";
    elseif($_POST['select_type']==2)
        $where_aid .=" and id={$_POST['select_id']} ";
}else if($_POST['select_text']!='')
    if($_POST['select_type']==1)
        $where_lid = " and instr(title,'{$_POST['select_text']}') ";
    elseif($_POST['select_type']==2)
        $where_aid .= " and instr(dname,'{$_POST['select_text']}') ";

//处理供应商类型
$_POST['com_type'];

//处理省市
if($_POST['city']+0>0){
    $where_lid .= " and areacode={$_POST['city']} ";
}
elseif($_POST['province']+0>0){
    $sql = "select n_code from pft_area_code_concat where parent_province={$_POST['province']}";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $n_code .= $row['n_code'].',';
    }
    $n_code = rtrim($n_code,',');
    $where_lid .= " and areacode in($n_code) ";
}

//统一查询景区 然后查tid
$lid = '';
if(strpos($where_lid,'and')){
    $sql = "select id from uu_land $where_lid";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $lid .= $row['id'].',';
    }
    $lid = rtrim($lid,',');
    if($lid=='') {
        $lid = '0';
        $post_tid = '0';
    }
}

if($lid!='' && $lid!='0'){
    $sql = "select id from uu_jq_ticket where landid in($lid)";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $post_tid .= $row['id'].',';
    }
    $post_tid = rtrim($post_tid,',');
}
//echo $post_tid;exit;
//统一查询供应商id
if(strpos($where_aid,'and')){
    $sql = "select id,creattime from pft_member $where_aid";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $post_aid .= $row['id'].',';
    }
    $post_aid = rtrim($post_aid,',');
    if($post_aid=='') $post_aid = '0';
}

foreach($group as $k=>$v){
    $v = explode(',',$v);
    $group_arr[$k] = $v;
}
$query = array(
    'btime'  =>$post_btime,
    'etime'  =>$post_etime,
    'qiandan'=>$post_qiandan,
    'aid'    =>$post_aid,
    'tid'    =>$post_tid,
    'group'  =>$post_group,
    'com_type'=>$_POST['com_type'],
    'kefu'   =>$_POST['kefu'],
    'chenping'  =>$chenping,
    'group_arr'=>json_encode($group_arr),
);

echo $return = curr($query);
exit;
function curr($query){
    $query['token'] = md5($query['btime'].'7667659191929397103470329788807862402627565924638280479623896');
    $url = "http://s.12301.cc/pft/order_report/product.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    return json_decode(curl_exec($ch),true);
    return curl_exec($ch);
}

